/* Send.java
 *
 * Project/Product: GWBS2009
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   严首骅	2009-5-27	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2009
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import com.apep.connect.APEPRequest;
import com.apep.connect.APEPResponse;
import com.apep.connect.APEPServiceException;
import com.apep.lib.xml.SampleElement;
import com.hitrust.gs.bean.GSAlignmentBean;
import com.hitrust.gs.bean.GSManufactureCategoryBean;
import com.hitrust.gs.business.GSBusAlignmentProcessor;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.LogWriter;

public class Send extends java.util.TimerTask {

    RongAiClient RongAiClient = new RongAiClient();

    public boolean isRun() {
        boolean rst = XMLSend.getInstance().isToRong();
        return rst;
    }

    public void run() {
        if (isRun()) {

            boolean rs = true;
            try {
                ArrayList tList = GSBusAlignmentProcessor.searchTask();
                boolean isCon = true;
                for (int i = 0; i < tList.size(); i++) {
                    if (isCon == false) {
                        continue;
                    }
                    GSAlignmentBean tGSAlignmentBean = (GSAlignmentBean) tList.get(i);
                    String errorInfo = "发送荣爱失败.";
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitOutWh")) {
                        rs = doOutWh(rs, tGSAlignmentBean, errorInfo);
                    }
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitInWh")) {
                        rs = doInWh(rs, tGSAlignmentBean, errorInfo);
                    }
                    //添加产品分类-物资类别
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitMaterialType")) {                       
                        rs = doMaterialType(rs, tGSAlignmentBean, errorInfo);                        
                    }
                    // 添加产品-物资基本信息
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitMaterialInfo")) {
                        rs = doMaterialInfo(rs, tGSAlignmentBean, errorInfo);
                    }
                    // 添加仓库-仓库信息
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitWareHouse")) {
                        rs = doWareHouseInfo(rs, tGSAlignmentBean, errorInfo);
                    }
                    // 添加供应商-供应商信息
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitProvider")) {
                        rs = doProviderInfo(rs, tGSAlignmentBean, errorInfo);
                    }
                    // 删除供应商-供应商信息
                    if (tGSAlignmentBean.getACTIONNAME().equals("SubmitProviderDelete")) {
                        rs = doProviderDelete(rs, tGSAlignmentBean, errorInfo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } 
            System.out.println("运行");
        }
        // System.out.println("未启用荣爱通讯");
    }

    /**
     * @param aRs
     * @param aAlignmentBean
     * @param aErrorInfo
     * @return
     */
    private boolean doMaterialInfo(boolean rs, GSAlignmentBean aAlignmentBean, String aErrorInfo) throws Exception {
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitMaterialInfo";
            String dataName = "MaterialInfo";
            // 传输xml to 荣爱
           rs =  XMLSend.getInstance().submitMaterialInfoSendXML(actionName, dataName, aAlignmentBean.getREMARK());
           if (!rs) {
               LogWriter.error(aErrorInfo);
           }

        }
        return rs;
    }
    private boolean doWareHouseInfo(boolean rs, GSAlignmentBean aAlignmentBean, String aErrorInfo) throws Exception{
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitWareHouse";
            String dataName = "WareHouse";
            // 传输xml to 荣爱
           rs =  XMLSend.getInstance().submitWareHouseSendXML(actionName, dataName, aAlignmentBean.getREMARK());
           if (!rs) {
               LogWriter.error(aErrorInfo);
           }

        }
        return rs;
    }
    private boolean doProviderInfo(boolean rs, GSAlignmentBean aAlignmentBean, String aErrorInfo) throws Exception {
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitProvider";
            String dataName = "Provider";
            // 传输xml to 荣爱
           rs =  XMLSend.getInstance().submitProviderSendXML(actionName, dataName, aAlignmentBean.getREMARK());
           if (!rs) {
               LogWriter.error(aErrorInfo);
           }
        }
        return rs;
    }
    private boolean doProviderDelete(boolean rs, GSAlignmentBean aAlignmentBean, String aErrorInfo) throws Exception{
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitProviderDelete";
            String dataName = "Provider";
            // 传输xml to 荣爱
           rs =  XMLSend.getInstance().submitProviderDeleteSendXML(actionName, dataName, aAlignmentBean.getREMARK());
           if (!rs) {
               LogWriter.error(aErrorInfo);
           }          
        }
        return rs;
    }

    /**
     * @param rs
     * @param tGSAlignmentBean
     * @param errorInfo
     * @return
     * @throws APEPServiceException
     * @throws UnsupportedEncodingException
     * @throws GSException
     */
    private boolean doMaterialType(boolean rs, GSAlignmentBean tGSAlignmentBean, String errorInfo) throws APEPServiceException, UnsupportedEncodingException,
            GSException {
        if (XMLSend.getInstance().isToRong()) {                                               
        String actionName = "SubmitMaterialType";
        String dataName = "MaterialType";
            // 传输xml to 荣爱
            rs = XMLSend.getInstance().submitMaterialTypeSendXML(actionName, dataName, tGSAlignmentBean.getREMARK());
            if (!rs) {
                LogWriter.error(errorInfo);
            }
        }
        return rs;
    }

    /**
     * @param rs
     * @param tGSAlignmentBean
     * @param errorInfo
     * @return
     * @throws APEPServiceException
     * @throws GSException
     */
    private boolean doInWh(boolean rs, GSAlignmentBean tGSAlignmentBean, String errorInfo) throws APEPServiceException, GSException {
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitInWh";
            String dataName = "InWh";
            // 传输xml to 荣爱
            rs = XMLSend.getInstance().submitInWhSendXML(actionName, dataName, tGSAlignmentBean.getREMARK());
            if (!rs) {
                LogWriter.error(errorInfo);
            }

        }
        return rs;
    }

    /**
     * @param rs
     * @param tGSAlignmentBean
     * @param errorInfo
     * @return
     * @throws APEPServiceException
     * @throws GSException
     */
    private boolean doOutWh(boolean rs, GSAlignmentBean tGSAlignmentBean, String errorInfo) throws APEPServiceException, GSException {
        if (XMLSend.getInstance().isToRong()) {
            String actionName = "SubmitOutWh";
            String dataName = "OutWh";
            // 传输xml to 荣爱
            rs = XMLSend.getInstance().submitOutWhSendXML(actionName, dataName, tGSAlignmentBean.getREMARK());
            if (!rs) {
                LogWriter.error(errorInfo);
            }
        }
        return rs;
    }

}
